
clear all
cls

local path_inputs 	= "00_data/01_datasets/00_inputs"
local path_save 	= "00_data/01_datasets/01_results"
local path_FTmain 	= "03_figures_tables/01_Main"


local Figure_3 	= 1
local Figure4_Table1 = 1
local Tables_2_3 	= 1


* ---------------------------------------------------------------------------------------------
* ------------------------------------------ FIGURE 3 -----------------------------------------
* ---------------------------------------------------------------------------------------------
if `Figure_3' == 1{
* ---------------------------------------------------------------------------------------------
* Panel A - Measures of Inflation [monthly frequency - not annualized]
use "`path_inputs'/02_Arg_merged.dta",clear
tset Date_Month, monthly 
keep if Date_Month>=ym(2006,1) & Date_Month<=ym(2013,1) 

twoway (connected IPC_INDEC       Date_Month,    yaxis(1)  ytitle("Monthly Inflation, %",  size(small)  axis(1)) xlabel(,labsize(small)) ylabel(, nogrid labsize(small) format(%9.0gc) axis(1))   lcolor(black)   mcolor(black)    msize(small)  plotregion(color(white)) graphregion(color(white)) lwidth(medthick))  ///
       (line IPC_FIEL        Date_Month,    yaxis(1)  lcolor(gs12)   mcolor(gray)   plotregion(color(white)) graphregion(color(white)) lwidth(vthin) lpattern(dash)) ///
       (line PriceStats      Date_Month,    yaxis(1)  lcolor(gs12)   mcolor(gray)   plotregion(color(white)) graphregion(color(white)) lwidth(vthin) lpattern(dash)) ///
       (line IPC7            Date_Month,    yaxis(1)  lcolor(gs12)   mcolor(gray)   plotregion(color(white)) graphregion(color(white)) lwidth(vthin) lpattern(dash)) ///
       (line IPC_BAC         Date_Month,    yaxis(1)  lcolor(gs12)   mcolor(gray)   plotregion(color(white)) graphregion(color(white)) lwidth(vthin) lpattern(dash)) ///
       (line IPC_Congreso    Date_Month,    yaxis(1)  lcolor(gs12)   mcolor(gray)   plotregion(color(white)) graphregion(color(white)) lwidth(vthin) lpattern(dash)) ///
	   (connected Inf_Private_AVG Date_Month,    yaxis(1)  lcolor(gs10)   msize(small) mcolor(gs10)   plotregion(color(white)) graphregion(color(white)) lwidth(medthick) lpattern(solid) ///
	   leg( order(1 "Official" 7 "Alternative")  size(small) rows(1)  pos(2) ring(0) nobox region(color(white))) ///
	   xtitle("Date", size(small))   xlabel(, labsize(small)) ///  
	   xlabel(`=ym(2006,1)' `=ym(2007,1)' `=ym(2008,1)' `=ym(2009,1)' `=ym(2010,1)' `=ym(2011,1)' `=ym(2012,1)' `=ym(2013,1)',  format(%tmCY)) ///
	   xline(`=ym(2007,1)', lcolor(gray) lpattern(dash)) )
	   qui graph export "`path_FTmain'/Figure_Inflation_monthly.pdf", replace
* --------------------------------------------------------------------------------


* --------------------------------------------------------------------------------  
* Panel B - Spreads, Argentina vs LAC  [daily frequency]
use "`path_inputs'/02_Arg_merged.dta",clear
keep if Date>=d(01jan2006) & Date<=d(30jan2013)

egen EMBI_LATAM_avg              = rowmean(EMBI_BRA EMBI_URU EMBI_PER EMBI_CHI EMBI_MEX)

twoway (line EMBI_ARG        Date,    yaxis(1)  ytitle("EMBI Spreads, bps",  size("`text_size'")  axis(1)) ylabel(, nogrid labsize("`text_size'") format(%9.0gc) axis(1))   lcolor(black)   mcolor(black)   plotregion(color(white)) graphregion(color(white)) lwidth(med))  ///
   	   (line EMBI_BRA        Date,    yaxis(1)  lcolor(gs10)   mcolor(gray)   plotregion(color(white)) graphregion(color(white)) lwidth(vthin)  lpattern(dash)) ///
   	   (line EMBI_URU        Date,    yaxis(1)  lcolor(gs10)   mcolor(gray)   plotregion(color(white)) graphregion(color(white)) lwidth(vthin)  lpattern(dash)) ///
   	   (line EMBI_PER        Date,    yaxis(1)  lcolor(gs10)   mcolor(gray)   plotregion(color(white)) graphregion(color(white)) lwidth(vthin)  lpattern(dash)) ///
   	   (line EMBI_CHI        Date,    yaxis(1)  lcolor(gs10)   mcolor(gray)   plotregion(color(white)) graphregion(color(white)) lwidth(vthin)  lpattern(dash)) ///
   	   (line EMBI_MEX        Date,    yaxis(1)  lcolor(gs10)   mcolor(gray)   plotregion(color(white)) graphregion(color(white)) lwidth(vthin)  lpattern(dash)) ///
	   (line EMBI_LATAM_avg  Date,    yaxis(1)  lcolor(gs10)   mcolor(gray)   plotregion(color(white)) graphregion(color(white)) lwidth(medthick) lpattern(solid) ///
	   leg( order(1 "Argentina" 7 "LATAM (avg)")  size(small) rows(3)  pos(2) ring(0) nobox region(color(white))) ///
	   xtitle("Date", size(small))   xlabel(, labsize(small)) ///  
   	   tlabel(01jan2006 01jan2007  01jan2008 01jan2009  01jan2010  01jan2011 01jan2012, format(%tdCY)) ///	
	   xline(`=d(01feb2007)', lcolor(gray) lpattern(dash)) )
	   qui graph export "`path_FTmain'/Figure_EMBI_LATAM.pdf", replace
*--------------------------------------------------------------------------------
}
* ---------------------------------------------------------------------------------------------
* ---------------------------------------------------------------------------------------------


*Note: Figure 4 & Table 1 are created in the "21_Heterosk_Analysis.do" file



* ---------------------------------------------------------------------------------------------
* --------------------------------------- TABLES 2 & 3 ----------------------------------------
* ---------------------------------------------------------------------------------------------
if `Tables_2_3'==1 {
clear
local DepVar_Vector      = "EMBI_ARG_Change Merval_Change"

* ------------------------------------------------------------------------------------------------
* IV REGRESSION RESULTS - FULL SAMPLE & BOOTSTRAP CI
* ------------------------------------------------------------------------------------------------
 
foreach DepVar in `DepVar_Vector'     {   // Loop for LHS variable (EMBI & Merval)	   
   	
	* Load Results for the full sample (alpha & number of observations)
	   forval i=0(1)3 {
		   use "`path_save'/20_IVReg_`DepVar'_Window_`i'.dta", clear
		   gen window = `i'
		   keep window Instrument beta obs
		   save tmp_file_`i'.dta,replace
	   }

	   use tmp_file_0.dta, clear
	   forval j=1(1)3 {
		   append using tmp_file_`j'
	   }
	   save tmp_file.dta, replace   
	   
   * Load Results for the Bootstrap simulations (confidence intervals)	  
	   forval i=0(1)3 {	   	
		   use "`path_save'/20_Bootstrap_`DepVar'_Window_`i'", clear
		   gen window = `i'
		   keep if _n==1
		   gen stars = ""
		   replace stars = "*"   if p_050*p_950>0
		   replace stars = "**"  if p_025*p_975>0
		   replace stars = "***" if p_005*p_995>0
		   keep window stars p_025 p_975
		   save tmp_file_boot_`i'.dta, replace
	   }
	   use tmp_file_boot_0.dta

	   forval j=1(1)3 {
		   append using tmp_file_boot_`j'
	   }
	   save tmp_file_boot.dta, replace
	   	   
   * Merge all the files created above
	   use tmp_file.dta,clear
	   merge 1:1 window using tmp_file_boot
	   drop _merge
   	   erase tmp_file_boot.dta

	   forval i=0(1)3{
		   erase  tmp_file_boot_`i'.dta
		   erase  tmp_file_`i'.dta
	   }
	   gen beta_star= string(beta,"%5.3f")+stars
	   gen CI = "["+string(p_025,"%5.2f")+", "+string(p_975,"%5.2f")+"]"
	
	* Create the table
		tempname table_IVreg
		file open table_IVreg using "`path_FTmain'/IV_Regression_`DepVar'.tex", write replace
		file write table_IVreg "\begin{tabular}{lcccc}" _n "\hline \hline" _n
		file write table_IVreg " & (1) & (2) & (3) & (4)  \\" _n  "\hline" _n	
		file write table_IVreg "$\Delta BE$" 	    "&" (beta_star[1]) " &" (beta_star[2])  " &" (beta_star[3]) " &" (beta_star[4])  " \\" _n
		file write table_IVreg "95perc CI"       	"&" (CI[1])        " &" (CI[2])         " &" (CI[3])        " &" (CI[4])         " \\" _n
		file write table_IVreg "Observations" "&" (string(obs[1], "%5.0f")) "&" (string(obs[2], "%5.0f"))  "&" (string(obs[3], "%5.0f"))  "&" (string(obs[4], "%5.0f"))  	   "\\"  _n
		file write table_IVreg "\hline" _n
		file write table_IVreg "Events     & 2-day window   & 3-day window    & 2-day window & 3-day window \\" _n	
		file write table_IVreg "Non-events & All other days & All other days  & 4-day window & 4-day window \\" _n	
		file write table_IVreg "Controls & Yes & Yes & Yes &  Yes \\" _n	
		file write table_IVreg "\hline \hline" _n
		file write table_IVreg "\end{tabular}" _n
		file close table_IVreg
	erase tmp_file.dta

}
* ------------------------------------------------------------------------------------------------
}
* ---------------------------------------------------------------------------------------------
* ---------------------------------------------------------------------------------------------	

	
